package com.zhhs.galaxyott.activity.mainActivity;

import ipaneltv.toolkit.fragment.PlayActivityInterface.VodPlayBaseInterface;
import ipaneltv.toolkit.fragment.VodPlayFragment;

import java.util.List;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.DeviceCenterActivity;
import com.zhhs.galaxyott.activity.HotMovieActivity;
import com.zhhs.galaxyott.activity.MoreSpecialActivity;
import com.zhhs.galaxyott.activity.SingleSpecialActivity;
import com.zhhs.galaxyott.activity.VPInterface.IMainView;
import com.zhhs.galaxyott.activity.VPInterface.PlayerCallback;
import com.zhhs.galaxyott.activity.moreMoviesActivity.MoreMoviesActivity;
import com.zhhs.galaxyott.activity.movieDetailActivity.MovieDetailActivity;
import com.zhhs.galaxyott.adapters.HomeMovieListAdapter;
import com.zhhs.galaxyott.beans.HomeMovieBean;
import com.zhhs.galaxyott.customview.myListView;
import com.zhhs.galaxyott.sharepreference.SharedPrefHelper;
import com.zhhs.galaxyott.utils.Logs;

@SuppressLint("NewApi")
public class MainHolder implements View.OnClickListener, IMainView, OnFocusChangeListener {

    private Context mContext;

    private static final int INIT_POSTER = 1000;
    private static final int INIT_PREVIEW = 1001;
    public static int CurrentListPostion = 0;

    View containerView;
    TextView push_txt;
    View home_movie;
    // VideoPlayView player;
    SurfaceView DVBplayer;
    ImageView loadImg;
    ImageView vip_tag, vip_sort, hotmovie_sort, moremovie_sort, home_movieImg;
    ImageView history, personal, topic_sort, activity_sort, preview_sort;

    VodPlayFragment fragment;
    VodPlayBaseInterface baseInterface;
    String uri;

    // 此版本没有用Listview来设置电影列表，如果要实现效果图中的效果则会出现反应迟钝的现象
    myListView mListView;
    HomeMovieListAdapter mAdapter;

    LinearLayout moviesLayout;
    TextView moviename_1, moviename_2, moviename_3, moviename_4, moviename_5, moviename_6, moviename_7;
    public static List<HomeMovieBean> listData;
    List<HomeMovieBean> listDatas;

    private boolean isvip = false;
    private String urlString;
    public static int movielistPosition = 0;
    private SharedPrefHelper sp;

    public MainHolder(MainActivity context) {
        sp = SharedPrefHelper.getInstance(context);
        this.mContext = context;
        uri = "rtsp://192.168.14.60/88888888/16/20160106/269921116/269921116.ts?rrsip=192.168.14.60&SRMSessionID=3448!778.000!3846!10208880001020193!9950000002100169!10.239.162.18&PurchaseToken=4495265&NetType=Cable&accountinfo=,9950000002100169,192.168.88.2,&SRMSIP=192.168.13.92&SRMSPORT=33700?/192.168.110.140:8082/6/778000/6875/64/H.264^null^-1^美国队长2^8151^1^1";
        initView();
        // initFragentPlay();
    }

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case INIT_POSTER:
                if (DVBplayer.isShown()) {
                    DVBplayer.setVisibility(View.GONE);
                    loadImg.setVisibility(View.GONE);
                    // player.stopPlayback();
                    home_movieImg.setVisibility(View.VISIBLE);
                }
                Glide.with(mContext).load(msg.obj).placeholder(R.drawable.homepage_poster_default).crossFade()
                        .into(home_movieImg);
                break;
            case INIT_PREVIEW:
                home_movieImg.setVisibility(View.GONE);
                // loadImg.setVisibility(View.VISIBLE);
                // loadImg.setImageResource(R.drawable.load_animation);
                // ((AnimationDrawable) loadImg.getDrawable()).start();

                DVBplayer.setVisibility(View.VISIBLE);

                // player.setPlayerCallbackListener(mCallback);
                // player.setVideoPath(url);

                break;

            default:
                break;
            }
        }

    };

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    private void initFragentPlay() {
        // TODO Auto-generated method stub
        fragment = VodPlayFragment.createInstance("30d283b4-605d-b0d8-cf5c-c0e783b7f348", VodPlayFragment.HUAWEI);
        baseInterface = fragment.getPlayInterface(new callback());
        ((Activity) mContext).getFragmentManager().beginTransaction().add(fragment, null);
    }

    private void initView() {

        containerView = ((Activity) mContext).findViewById(R.id.container);
        push_txt = (TextView) ((Activity) mContext).findViewById(R.id.push_txt);
        push_txt.setSelected(true);

        vip_tag = (ImageView) ((Activity) mContext).findViewById(R.id.vip_tag);
        vip_tag.setVisibility(View.INVISIBLE);

        home_movie = ((Activity) mContext).findViewById(R.id.homepage_movie);
        loadImg = (ImageView) ((Activity) mContext).findViewById(R.id.load_bar);
        loadImg.setVisibility(View.GONE);

        home_movieImg = (ImageView) ((Activity) mContext).findViewById(R.id.homepage_movieImg);
        // player = (VideoPlayView) ((Activity)
        // mContext).findViewById(R.id.homepage_movieVideo);
        DVBplayer = (SurfaceView) ((Activity) mContext).findViewById(R.id.homepage_movieVideo);
        // initPreview(urlString, "1");

        vip_sort = (ImageView) ((Activity) mContext).findViewById(R.id.homepage_vip);
        hotmovie_sort = (ImageView) ((Activity) mContext).findViewById(R.id.homepage_hotmovie);
        moremovie_sort = (ImageView) ((Activity) mContext).findViewById(R.id.homepage_moremovie);
        history = (ImageView) ((Activity) mContext).findViewById(R.id.homepage_history);
        personal = (ImageView) ((Activity) mContext).findViewById(R.id.homepage_personal);
        topic_sort = (ImageView) ((Activity) mContext).findViewById(R.id.homepage_topic);
        activity_sort = (ImageView) ((Activity) mContext).findViewById(R.id.homepage_activity);
        preview_sort = (ImageView) ((Activity) mContext).findViewById(R.id.homepage_preview);

        moviesLayout = (LinearLayout) ((Activity) mContext).findViewById(R.id.homepage_movieList);
        // initMovieLayout();

        vip_sort.requestFocus();

        vip_sort.setOnClickListener(this);
        hotmovie_sort.setOnClickListener(this);
        moremovie_sort.setOnClickListener(this);
        history.setOnClickListener(this);
        personal.setOnClickListener(this);

    }

    private int objectid1, objectid2, objectid3;

    public void setListener(int pos, int type, int id) {
        if (pos == 1) {
            this.objectid1 = id;
            topic_sort.setOnClickListener(this);
            topic_sort.setTag(type);
        } else if (pos == 2) {
            this.objectid2 = id;
            activity_sort.setOnClickListener(this);
            activity_sort.setTag(type);
        } else if (pos == 3) {
            this.objectid3 = id;
            preview_sort.setOnClickListener(this);
            preview_sort.setTag(type);
        }

    }

    public void initMovieLayout(int size) {

        // 此处子字条目写死的布局
        moviename_1 = (TextView) ((Activity) mContext).findViewById(R.id.moviename_1);
        moviename_2 = (TextView) ((Activity) mContext).findViewById(R.id.moviename_2);
        moviename_3 = (TextView) ((Activity) mContext).findViewById(R.id.moviename_3);
        moviename_4 = (TextView) ((Activity) mContext).findViewById(R.id.moviename_4);
        moviename_5 = (TextView) ((Activity) mContext).findViewById(R.id.moviename_5);
        moviename_6 = (TextView) ((Activity) mContext).findViewById(R.id.moviename_6);
        moviename_7 = (TextView) ((Activity) mContext).findViewById(R.id.moviename_7);

        moviename_1.setOnClickListener(this);
        moviename_2.setOnClickListener(this);
        moviename_3.setOnClickListener(this);
        moviename_4.setOnClickListener(this);
        moviename_5.setOnClickListener(this);
        moviename_6.setOnClickListener(this);
        moviename_7.setOnClickListener(this);

        moviename_1.setOnFocusChangeListener(this);
        moviename_2.setOnFocusChangeListener(this);
        moviename_3.setOnFocusChangeListener(this);
        moviename_4.setOnFocusChangeListener(this);
        moviename_5.setOnFocusChangeListener(this);
        moviename_6.setOnFocusChangeListener(this);
        moviename_7.setOnFocusChangeListener(this);

        for (int i = 0; i < size; i++) {
            getMoiveList(i).setVisibility(View.VISIBLE);
        }

        int count = moviesLayout.getChildCount();
        Logs.logD("DEBUG_LL", "THIS IS initMovieLayout-->" + count);
        if (moviename_4.isShown()) {
            setNextFocus(moviename_4);
            moviename_4.setTextColor(mContext.getResources().getColor(R.color.text_white));
        }

    }

    @Override
    public void onFocusChange(View v, boolean hasfocus) {
        if (hasfocus) {
            v.setSelected(true);
        } else {
            v.setSelected(false);
        }

    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent();

        switch (v.getId()) {
        case R.id.homepage_movie:
            Logs.logD("debug", "this is on onClick--》" + (Integer) v.getTag());
            intent.setClass(mContext, MovieDetailActivity.class);
            intent.putExtra("movieid", (Integer) v.getTag());
            mContext.startActivity(intent);
            break;
        // VIP专属
        case R.id.homepage_vip:
            intent.setClass(mContext, MoreMoviesActivity.class);
            intent.putExtra("type", 1);
            mContext.startActivity(intent);
            break;
        // 热门影片
        case R.id.homepage_hotmovie:
            intent.setClass(mContext, HotMovieActivity.class);
            intent.putExtra("specailId", 1);
            mContext.startActivity(intent);
            break;
        // 更多影片
        case R.id.homepage_moremovie:
            intent.setClass(mContext, MoreMoviesActivity.class);
            intent.putExtra("type", 0);
            mContext.startActivity(intent);
            break;
        case R.id.homepage_history:
            if (sp.getIsComboOnline()) {
                intent.setClass(mContext, DeviceCenterActivity.class);
                intent.putExtra("number", 1);
                mContext.startActivity(intent);
            } else {
                intent.setClass(mContext, DeviceCenterActivity.class);
                intent.putExtra("number", 0);
                mContext.startActivity(intent);
            }

            break;
        case R.id.homepage_personal:
            if (sp.getIsComboOnline()) {
                intent.setClass(mContext, DeviceCenterActivity.class);
                intent.putExtra("number", 0);
                mContext.startActivity(intent);
            } else {
                intent.setClass(mContext, DeviceCenterActivity.class);
                intent.putExtra("number", 2);
                mContext.startActivity(intent);
            }

            break;
        case R.id.homepage_topic:

            startActivity((Integer) v.getTag(), objectid1);

            break;
        case R.id.homepage_activity:
            startActivity((Integer) v.getTag(), objectid2);

            break;
        case R.id.homepage_preview:
            startActivity((Integer) v.getTag(), objectid3);

            break;
        case R.id.moviename_1:
            itemStartActivity(0);

            break;
        case R.id.moviename_2:
            itemStartActivity(1);
            break;
        case R.id.moviename_3:
            itemStartActivity(2);
            break;
        case R.id.moviename_4:
            itemStartActivity(3);
            break;
        case R.id.moviename_5:
            itemStartActivity(4);
            break;
        case R.id.moviename_6:
            itemStartActivity(5);
            break;
        case R.id.moviename_7:
            itemStartActivity(6);
            break;

        default:
            break;
        }

    }

    public void itemStartActivity(int pos) {
        Intent intent = new Intent();
        if (listData.get(pos).getAdType() == 1) {
            intent.setClass(mContext, MovieDetailActivity.class);
            intent.putExtra("movieid", listData.get(pos).getObjectid());
            mContext.startActivity(intent);
        } else if (listData.get(pos).getAdType() == 2) {

        }
    }

    public void startActivity(int type, int objectid) {
        Intent intent = new Intent();
        if (type == 1) {
            intent.setClass(mContext, MovieDetailActivity.class);
            intent.putExtra("movieid", objectid);
            mContext.startActivity(intent);

        } else if (type == 2) {
            // intent.setClass(mContext, HotMovieActivity.class);
            // this.startActivity(intent);

        } else if (type == 3) {
            intent.setClass(mContext, MoreSpecialActivity.class);
            mContext.startActivity(intent);

        } else if (type == 4) {
            Logs.logD("debug", "this is startActivity-->4:" + objectid);
            intent.setClass(mContext, SingleSpecialActivity.class);
            intent.putExtra("specailId", objectid);
            mContext.startActivity(intent);

        } else if (type == 5) {
            Logs.logD("debug", "this is startActivity-->5:" + objectid);
            intent.setClass(mContext, HotMovieActivity.class);
            intent.putExtra("specailId", objectid);
            mContext.startActivity(intent);

        }
    }

    @Override
    public void showData(List<HomeMovieBean> listData) {

        for (int i = 0; i < listData.size(); i++) {
            (getTextView(i)).setText(listData.get(i).getAdTitle());
        }

        this.listData = listData;

        // 防止没数据没网络的时候报错
        home_movie.setOnClickListener(this);

        if (listData == null || listData.size() == 0) {
            return;
        }
        if (listData.size() > 3) {
            HomeMovieBean homeMovieBean = listData.get(3);
            home_movie.setTag(listData.get(3).getObjectid());
            initPreview(homeMovieBean.getImageUrl(), homeMovieBean.getAdType());
        }

    }

    public TextView getTextView(int i) {
        return (TextView) ((RelativeLayout) moviesLayout.getChildAt(i)).getChildAt(0);
    }

    public RelativeLayout getMoiveList(int i) {
        return (RelativeLayout) moviesLayout.getChildAt(i);
    }

    @Override
    public void setNextFocus(View view) {
        home_movie.setNextFocusRightId(view.getId());
        history.setNextFocusLeftId(view.getId());
        topic_sort.setNextFocusLeftId(view.getId());
        activity_sort.setNextFocusLeftId(view.getId());
        moremovie_sort.setNextFocusUpId(view.getId());

    }

    // 此处上述布局固定的设置对应
    @Override
    public void setMovieSelect(String string) {
        // 1代表向下
        if (string.equals("1")) {
            for (int i = 0; i < listData.size() - 1; i++) {
                if (moviesLayout.findFocus() == getTextView(i)) {
                    getTextView(i).setTextColor(mContext.getResources().getColor(R.color.homepage_list_line));
                    getTextView(i + 1).setTextColor(mContext.getResources().getColor(R.color.text_white));
                    initPreview(listData.get(i + 1).getImageUrl(), listData.get(i + 1).getAdType());
                    home_movie.setTag(listData.get(i + 1).getObjectid());
                }

            }

        } else {
            for (int i = 1; i < listData.size(); i++) {
                if (moviesLayout.findFocus() == getTextView(i)) {
                    getTextView(i).setTextColor(mContext.getResources().getColor(R.color.homepage_list_line));
                    getTextView(i - 1).setTextColor(mContext.getResources().getColor(R.color.text_white));
                    initPreview(listData.get(i - 1).getImageUrl(), listData.get(i - 1).getAdType());
                    home_movie.setTag(listData.get(i - 1).getObjectid());
                }
            }

        }

    }

    @Override
    public void initPreview(String url, int type) {
        if (url != null && type == 2) {

            Message msg = mHandler.obtainMessage(INIT_PREVIEW, url);
            msg.sendToTarget();

        } else if (type == 1) {

            Message msg = mHandler.obtainMessage();
            msg.what = INIT_POSTER;
            msg.obj = url;
            msg.sendToTarget();

        }

    }

    class callback implements VodPlayBaseInterface.Callback {

        @Override
        public void onContextReady(String group) {
            baseInterface.start(uri, 3, 1, 0);
            baseInterface.setDisplay(0, 0, 1280, 720);
            baseInterface.setVolume(1);
        }

        @Override
        public void onVodDuration(long d) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSeeBackPeriod(long s, long e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPlayStart(boolean b) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSourceRate(float r) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSourceSeek(long t) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSyncMediaTime(long t) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPlayEnd(float r) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPlayMsg(String msg) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPlayError(String msg) {
            // TODO Auto-generated method stub

        }

    }

    PlayerCallback mCallback = new PlayerCallback() {

        public void onPrepared(int width, int height) {
            Logs.logD("DEBUG_LC", "THIS IS onPrepared-->");
            // player.start();
            // loadImg.setVisibility(View.GONE);
        }

        public boolean onInfo(int what, int extra) {
            Logs.logD("DEBUG_LC", "THIS IS onInfo-->");
            return false;
        }

        public void onCompletion() {

        }

        public boolean onError(int framework_err, int impl_err) {
            return false;
        }

    };

}
